<!--教师登录页-->
<template>
  <div class="login-container">
    <el-card class="login-card">
      <h2 class="login-title">教师登录</h2>
      <el-form
        :model="loginForm"
        ref="loginForm"
        :rules="loginRules"
        label-width="100px"
        label-position="right"
      >
        <!-- 用户名 -->
        <el-form-item label="用户名:">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
            style="width: 400px"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="密码:">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            show-password
            style="width: 400px"
          ></el-input>
        </el-form-item>
        <!-- 验证码输入框 -->
        <el-form-item label="验证码:" prop="code">
          <el-input
            type="text"
            auto-complete="false"
            v-model="loginForm.code"
            placeholder="请输入验证码"
            style="width: 200px; margin-right: 1px"
          >
          </el-input>
        </el-form-item>
        <!-- 验证码显示框 -->
        <el-form-item>
          <div @click="refreshCode()">
            <s-identify :identifyCode="identifyCode"></s-identify>
          </div>
        </el-form-item>
        <!-- 登录 -->
        <el-form-item>
          <el-button type="primary" class="login-btn" @click="handleLogin"
          >登录
          </el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import service from "@/utils/service";
import SIdentify from "@/components/SIdentify.vue"; // 导入验证码组件
export default {
  components: {SIdentify},
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
        code: "",
      },
      loginRules: {
        username: [
          {required: true, message: "请输入用户名", trigger: "blur"},
        ],
        password: [{required: true, message: "请输入密码", trigger: "blur"}],
        code: [{required: true, message: "请输入验证码", trigger: "blur"}],
      },
      identifyCode: "",
      identifyCodes: "123456789abcdwershdjeJKDHRJHKPLMKQX", //绘制的随机数
    };
  },
  computed:{
    currentJumpPath(){
      const parts  = this.$route.path.split('/');
      const path = '/'+parts[1]+'/'+'InfoForm';
      console.log(path);
      return path;

    }

  },
  methods: {
    //处理登录操作
    async handleLogin() {
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          // 验证码正确才执行登录操作
          //将输入的验证码和真实的验证码都转化为小写，实现不区分大小写
          const enteredCode = this.loginForm.code.toLowerCase();
          const actualCode = this.identifyCode.toLowerCase();
          if (enteredCode === actualCode) {
            try {
              console.log("开始验证用户登录");
              const apiUrl =
                "/teachers/login";
              const response = await service.post(apiUrl, {
                name: this.loginForm.username,
                password: this.loginForm.password,
              });
              if (response.data.code === 20011) {
                // 登录成功，将 token 存储到 Vuex 中
                const token = response.data.data;
                this.$store.commit("setToken", token);
                // 跳转到教师个人信息页面
                this.$router.push({
                  path: this.currentJumpPath,
                });
              } else {
                console.log("登录失败：", response.data.msg);
                this.$notify.error(response.data.msg)
                this.refreshCode();
                this.loginForm.code = '';
              }
            } catch (error) {
              console.error("登录请求出错：", error);
            }
          } else {
            // 验证码不正确，提示用户重新输入验证码
            this.$message.error("验证码错误，请重新输入");
            // 重新生成验证码
            this.refreshCode();
            this.loginForm.code = '';
          }
        }
      });
    },

    //刷新验证码
    refreshCode() {
      this.identifyCode = "";
      this.makeCode(this.identifyCodes, 4);
    },
    //生成随机数
    randomNum(min, max) {
      max = max + 1;
      return Math.floor(Math.random() * (max - min) + min);
    },
    //随机生成验证码字符串
    makeCode(data, len) {
      for (let i = 0; i < len; i++) {
        this.identifyCode += data[this.randomNum(0, data.length - 1)];
      }
    },
  },
  created() {
    this.refreshCode();
  },
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  padding: 20px 0;
}

.login-card {
  width: 600px;
  height: 500px;
  padding: 20px 20px;

  /* box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1); */
}

.login-title {
  font-size: 24px;
  text-align: center;
  margin-bottom: 20px;
}

.login-btn {
  width: 50%;
  margin-left: 20px;
}

.login-container >>> .el-card {
  border: none;
}

/* .el-card.is-always-shadow{
  box-shadow: none;
} */
</style>
